package com.yt.sys.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import com.yt.sys.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yt.sys.mapper.PermissionMapper;
import com.yt.sys.models.service.IPermissionService;
import com.yt.sys.models.service.IRolePermissionService;
import com.yt.sys.models.service.IEmployeeRoleService;
import com.yt.sys.models.bo.PermissionSaveBO;
import com.yt.sys.models.dto.PermissionDTO;
import com.yt.sys.models.dto.RolePermissionDTO;
import com.yt.sys.models.dto.EmployeeRoleDTO;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 权限信息服务实现
 *
 * @author yzz
 * @Date 2025-10-22 09:47:20
 */
@DubboService
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, PermissionDTO> implements IPermissionService{

    @Resource
    private YmlConfig ymlConfig;
    
    @Resource
    private IEmployeeRoleService employeeRoleService;
    
    @Resource
    private IRolePermissionService rolePermissionService;

    @Override
    public boolean add(PermissionSaveBO saveBO) {
        PermissionDTO dto = new PermissionDTO();
        BeanUtils.copyProperties(saveBO, dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }
    
    @Override
    public boolean update(PermissionSaveBO saveBO) {
        PermissionDTO dto = new PermissionDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(Long.valueOf(saveBO.getId()));
        return this.updateById(dto);
    }
    
    @Override
    public List<PermissionDTO> getUserPermissions(String employeeId) {
        if (employeeId == null || employeeId.isEmpty()) {
            return new ArrayList<>();
        }
        
        // 1. 查询用户角色
        Wrapper<EmployeeRoleDTO> roleWrapper = new EntityWrapper<>();
        roleWrapper.eq("employee_id", employeeId);
        List<EmployeeRoleDTO> employeeRoles = employeeRoleService.selectList(roleWrapper);
        
        if (CollectionUtils.isEmpty(employeeRoles)) {
            return new ArrayList<>();
        }
        
        // 2. 获取角色ID列表
        List<String> roleIds = employeeRoles.stream()
                .map(EmployeeRoleDTO::getRoleId)
                .collect(Collectors.toList());
        
        // 3. 查询角色权限关联
        Wrapper<RolePermissionDTO> rolePermissionWrapper = new EntityWrapper<>();
        rolePermissionWrapper.in("role_id", roleIds);
        List<RolePermissionDTO> rolePermissions = rolePermissionService.selectList(rolePermissionWrapper);
        
        if (CollectionUtils.isEmpty(rolePermissions)) {
            return new ArrayList<>();
        }
        
        // 4. 获取权限ID列表
        List<String> permissionIds = rolePermissions.stream()
                .map(RolePermissionDTO::getPermissionId)
                .collect(Collectors.toList());
        
        // 5. 查询权限详情
        Wrapper<PermissionDTO> permissionWrapper = new EntityWrapper<>();
        permissionWrapper.in("id", permissionIds);
        return this.selectList(permissionWrapper);
    }
}